package com.ruoyi.common.utils.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RabbitConfig;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * rabbitmq发送消息工具类(生产者)
 */
@Component
public class RabbitSendUtils {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 将消息广播到指定乘客
     *
     * @param userId websocket中唯一id
     * @param msg    websocket推送的消息
     */
    public void sendMessage(Integer userId, String msg) {
        String message = userId.toString() + "#SPLIT#" + msg;
        // 把消息对象放入路由对应的队列当中去
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitConfig.msg_exchang,
                RabbitConfig.msg_routing_key,
                JSONObject.toJSON(message).toString(),
                correlationId);
    }

}
